﻿using MusicFluent.Models;
using MusicFluent.Pages;
using MusicFluent.Views;
using NetEasyMusic.Lib.Data;
using NetEasyMusic.Lib.Details;
using NetEasyMusic.Lib.Models;

namespace MusicFluent.MusicViews;

public partial class PlaylistView : PageViewBase
{
    private readonly PlaylistModel? _playlistModel;
    private readonly string? _cookie;
    private readonly PlaylistDetailModel? _playlistDetailModel;

    public PlaylistView(PlaylistModel playlist, string cookie)
    {
        _playlistModel = playlist;
        _cookie = cookie;
        InitializeComponent();
    }

    public PlaylistView(PlaylistDetailModel model)
    {
        _playlistDetailModel = model;
        InitializeComponent();
    }

    protected override async void OnInitialized()
    {
        base.OnInitialized();
        
        if (_playlistModel == null && _playlistDetailModel != null)
        {
            SongList.ItemsSource = _playlistDetailModel.Musics;
            if (string.IsNullOrEmpty(_playlistDetailModel.CoverImgUrl))
                PlaylistImage.IsVisible = false;
            else
                PlaylistImage.Source = $"{_playlistDetailModel.CoverImgUrl}?param=300y300";

            DescBlock.Text = _playlistDetailModel.Description;
            NameBlock.Text = _playlistDetailModel.Name;
        }
        else
        {
            var main = ViewOpera.GetView<MainWindow>(this);
            using var music = new Playlist(_cookie ?? "");
            main?.TaskShow();
            var model = await music.GetPlaylist(_playlistModel!);
            main?.TaskShow();
            if (string.IsNullOrEmpty(model.CoverImgUrl))
            {
                PlaylistImage.IsVisible = false;
                return;
            }

            PlaylistImage.Source = $"{model.CoverImgUrl}?param=300y300";
            SongList.ItemsSource = model.Musics;
            DescBlock.Text = model.Description;
            NameBlock.Text = model.Name;
        }
    }
}